package tcts0421;

public class 双指针209长度最小的子数组0513 {

        public int minSubArrayLen(int target, int[] nums) {
            int preSum = 0;
            int res = Integer.MAX_VALUE;
            for(int i=0,j=0;i<nums.length;i++){
                preSum += nums[i];
                while(preSum - nums[j]>=target){
                    preSum-=nums[j];
                    j++;
                }
                if(preSum>=target){
                    res = Math.min(res,i-j+1);
                }
            }
            return res==Integer.MAX_VALUE?0:res;
        }

    public int minSubArrayLen1(int s, int[] nums) {
        int left = 0;
        int sum = 0;
        int res = Integer.MAX_VALUE;
        for (int right = 0; right < nums.length; right++) {
            sum += nums[right];
            while (sum >= s) {
                res = Math.min(res, right - left + 1);
                sum -= nums[left++];
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }


    public static void main(String[] args) {
        System.out.println(new 双指针209长度最小的子数组0513().minSubArrayLen(7, new int[]{2, 3, 1, 2, 4, 3}));
    }
}
